package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Focus;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@DubboService
public class FocusApiImpl implements FocusApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void focus(Focus focus) {
        mongoTemplate.save(focus);

    }

    @Override
    public boolean checkFocus(Long userId, Long toUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("toUserId").is(toUserId);
        Query query = Query.query(criteria);
        boolean focused = mongoTemplate.exists(query, Focus.class);
        System.out.println("测试是否关注"+focused+"!!!!!!!!!!!!!!!!!!!!!");
        return focused;
    }

    @Override
    public void unFocus(Long userId, Long toUserId) {
        Criteria criteria = Criteria.where("userId").is(userId).and("toUserId").is(toUserId);
        Query query = Query.query(criteria);
        mongoTemplate.remove(query, Focus.class);
    }
}
